using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Appcontext.CommandHandler
{
	public class GetInpatientOrderTemplateByIdQueryHandler : IRequestHandler<GetInpatientOrderTemplateByIdQuery, InpatientOrderTemplateDto>
	{
		private readonly IBaseRepository<InpatientOrderTemplate> _repository;

		public GetInpatientOrderTemplateByIdQueryHandler(IBaseRepository<InpatientOrderTemplate> repository)
		{
			_repository = repository;
		}

		public async Task<InpatientOrderTemplateDto> Handle(GetInpatientOrderTemplateByIdQuery request, CancellationToken cancellationToken)
		{
			var entity = await _repository.GetModel(x => x.TemplateId == request.TemplateId && !x.IsDeleted);
			if (entity == null) throw new Exception("模板不存在");

			return new InpatientOrderTemplateDto
			{
				TemplateId = entity.TemplateId,
				TemplateName = entity.TemplateName,
				Description = entity.Description,
				IsActive = entity.IsActive,
				IsDefault = entity.IsDefault,
				ShowSerialNumber = entity.ShowSerialNumber,
				ShowDepartment = entity.ShowDepartment,
				ShowDoctor = entity.ShowDoctor,
				ShowPrescription = entity.ShowPrescription,
				ShowBedNumber = entity.ShowBedNumber,
				ShowConsultationTime = entity.ShowConsultationTime,
				ShowAdmissionTime = entity.ShowAdmissionTime,
				ShowDischargeTime = entity.ShowDischargeTime,
				ShowExpenseDetails = entity.ShowExpenseDetails,
				ShowCashier = entity.ShowCashier,
				ShowAmount = entity.ShowAmount,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
